import express from 'express';
import { FileStorage } from '../FileStorage';
import type { Session } from '../FileStorage';

const router = express.Router();

// 获取所有会话
router.get('/sessions', async (req, res) => {
  try {
    const fileStorage = new FileStorage();
    await fileStorage.init();
    const sessions = await fileStorage.getEntities<Session>('sessions');
    res.json({
      code: 200,
      data: sessions,
      message: 'success'
    });
  } catch (error) {
    console.error('获取会话列表失败:', error);
    res.status(500).json({
      code: 500,
      error: '获取会话列表失败'
    });
  }
});

// 根据ID获取会话
router.get('/sessions/:id', async (req:any, res:any) => {
  try {
    const { id } = req.params;
    const fileStorage = new FileStorage();
    await fileStorage.init();
    const session = await fileStorage.findEntityById<Session>('sessions', id);
    
    if (!session) {
      return res.status(404).json({
        code: 404,
        error: '未找到指定的会话'
      });
    }
    
    res.json({
      code: 200,
      data: session,
      message: 'success'
    });
  } catch (error) {
    console.error('获取会话失败:', error);
    res.status(500).json({
      code: 500,
      error: '获取会话失败'
    });
  }
});

// 创建新会话
router.post('/sessions', async (req, res) => {
  try {
    const sessionData: Session = req.body;
    const fileStorage = new FileStorage();
    await fileStorage.init();
    const newSession = await fileStorage.addEntity<Session>('sessions', sessionData);
    
    res.status(201).json({
      code: 201,
      data: newSession,
      message: '会话创建成功'
    });
  } catch (error) {
    console.error('创建会话失败:', error);
    res.status(500).json({
      code: 500,
      error: '创建会话失败'
    });
  }
});

// 更新会话
router.put('/sessions/:id', async (req:any, res:any) => {
  try {
    const { id } = req.params;
    const updateData: Partial<Session> = req.body;
    const fileStorage = new FileStorage();
    await fileStorage.init();
    const updatedSession = await fileStorage.updateEntity<Session>('sessions', id, updateData);
    
    if (!updatedSession) {
      return res.status(404).json({
        code: 404,
        error: '未找到指定的会话'
      });
    }
    
    res.json({
      code: 200,
      data: updatedSession,
      message: '会话更新成功'
    });
  } catch (error) {
    console.error('更新会话失败:', error);
    res.status(500).json({
      code: 500,
      error: '更新会话失败'
    });
  }
});

// 删除会话
router.delete('/sessions/:id', async (req:any, res:any) => {
  try {
    const { id } = req.params;
    const fileStorage = new FileStorage();
    await fileStorage.init();
    const result = await fileStorage.deleteEntity('sessions', id);
    
    if (!result) {
      return res.status(404).json({
        code: 404,
        error: '未找到指定的会话'
      });
    }
    
    res.json({
      code: 200,
      message: '会话删除成功'
    });
  } catch (error) {
    console.error('删除会话失败:', error);
    res.status(500).json({
      code: 500,
      error: '删除会话失败'
    });
  }
});

export default router;